package com.flex.core.event.img
{
	import com.flex.core.constants.GameConstants;
	import com.flex.core.model.img.Mimage;
	import com.flex.func.grid.GridUtil;
	import com.flex.func.menu.MenuUtils;

	import flash.display.Sprite;
	import flash.events.Event;

	import mx.containers.Canvas;
	import mx.containers.VBox;
	import mx.controls.Alert;


	public class MimageEvent extends Sprite
	{
		public var mimage:Mimage;

		/**
		 * you have to add listener to listen you terminal function
		 */
		public function MimageEvent(mimage:Mimage)
		{
			this.mimage=mimage;
			this.addEventListener("test", test);
			this.addEventListener("resizeMap", resizeMap);
			this.addEventListener("resizeChangedMap", resizeChangedMap);
		}

		/**
		 * resize Map has been edited
		 */
		public function resizeChangedMap(e:Event):void
		{
			resizeMap(e, true);
		}

		/**
		 * resize Map, width and height must be multiple(or half,like:1、1.5、2、2.5、3)
		 */
		public function resizeMap(e:Event, isChange:Boolean=false):void
		{
			var canv:Canvas=mimage.parent as Canvas;
			var imgWidth:Number=mimage.content.width;
			var imgHeight:Number=mimage.content.height;
			var tempWidth:Number=imgWidth * 2;
			var tempHeight:Number=imgHeight * 2;
			if (tempWidth % GameConstants.BASE_GRID_WIDTH != 0 || tempHeight % GameConstants.BASE_GRID_HEIGHT != 0)
			{
				Alert.show("抱歉，图片尺寸有问题。必须高为您制定格子高度的n或(n+0.5)倍，宽为您制定格子宽度的n或(n+0.5)倍");
				return;
			}
			mimage.width=imgWidth;
			mimage.height=imgHeight;
			var imgScale:Number=imgHeight / imgWidth;
			var baseSacle:Number=GameConstants.MAP_AREA_HEIGHT / GameConstants.MAP_AREA_WIDTH;
			var scale:Number=1;
			if (imgScale < baseSacle)
			{ //rescale by width
				scale=GameConstants.MAP_AREA_WIDTH / imgWidth;
			}
			else
			{ //rescale by height
				scale=GameConstants.MAP_AREA_HEIGHT / imgHeight;
			}
			mimage.scaleX=scale;
			mimage.scaleY=scale;
			var parentCanv:Canvas=canv.parent as Canvas;
			var gridCanv:Canvas=parentCanv.getChildAt(1) as Canvas;
			if (!isChange)
			{
				GridUtil.createMapGrid(gridCanv, imgWidth, imgHeight, scale);
			}
			else
			{
				GridUtil.createChangedMapGrid(mimage.source.toString(), gridCanv, scale);
			}
			// create map menu
			var menuCanv:Canvas=new Canvas();
			var vbox:Canvas=parentCanv.parent as Canvas;
			var box:VBox=vbox.parent as VBox;
			box.addChild(menuCanv);
			menuCanv.graphics.beginFill(0xFFDDFF);
			menuCanv.graphics.drawRect(-GameConstants.MAP_AREA_WIDTH / 2, 0, GameConstants.MAP_AREA_WIDTH, GameConstants.MAP_MENU_HEIGHT);
			menuCanv.graphics.endFill();

			MenuUtils.addMapMenu(menuCanv);
		}



		/**
		 * you have to design your terminal function, one function for one kind of your use,
		 * this is a demo, if you want to call this function , you have to write these code,
		 * ImgUtils.getImage(data, "test"), see more from ImgUtils
		 */
		public function test(e:Event):void
		{
			Alert.show(mimage.content.width + ":" + mimage.content.height);
		}
	}
}